//
//  HZMSubject_DetailsViewController.m
//  糖糖大魔王
//
//  Created by 韩震明 on 15/10/26.
//  Copyright © 2015年 韩震明. All rights reserved.
//

#import "HZMSubject_DetailsViewController.h"

@interface HZMSubject_DetailsViewController ()<UICollectionViewDataSource,UICollectionViewDelegate>

@property(nonatomic, retain) NSMutableArray *dataArr;
@property(nonatomic, retain) NSMutableArray *productArr;
@property(nonatomic, retain) UICollectionView *collection;

@end

@implementation HZMSubject_DetailsViewController

- (void)dealloc
{
    [_list release];
    [_dataArr retain];
    [_productArr release];
    [_collection release];
    [super dealloc];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    
    [self setNavigation];
    
    [self createCollection];
    
    [self createData];
    
    // 可以写上下刷新, 还有hud等待, 头部可以加collecView联动
}

#pragma mark CollectionView Setting
- (void)createCollection
{
    UICollectionViewFlowLayout *flowLayout = [[[UICollectionViewFlowLayout alloc] init] autorelease];
    flowLayout.itemSize = CGSizeMake((WIDTH - 10) / 2, HEIGHT / 3);
    flowLayout.sectionInset = UIEdgeInsetsMake(5, 0, 0, 0);
    
    self.collection = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT - 64) collectionViewLayout:flowLayout];
    [self.view addSubview:self.collection];
    [_collection release];
    self.collection.backgroundColor = [UIColor whiteColor];
    self.collection.delegate = self;
    self.collection.dataSource = self;
    
    [self.collection registerClass:[HZMSingleArticleCollectionViewCell class] forCellWithReuseIdentifier:@"collectionReuse"];
    
}

#pragma mark CollectionView Delegate Method
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.productArr.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    HZMSingleArticleCollectionViewCell *cell = [self.collection dequeueReusableCellWithReuseIdentifier:@"collectionReuse" forIndexPath:indexPath];
    [cell.topImageView sd_setImageWithURL:[NSURL URLWithString:[self.productArr[indexPath.row] pic]] placeholderImage:[UIImage imageNamed:@"back.gif"]];
    cell.priceLabel.text = [self.productArr[indexPath.row] price];
    cell.titleLabel.text = [self.productArr[indexPath.row] title];
    cell.contentView.backgroundColor = [UIColor whiteColor];
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    HZMBaseWebView *itemVC = [[HZMBaseWebView alloc] init];
    [self.navigationController pushViewController:itemVC animated:YES];
    itemVC.WebViewStrUrl = [self.productArr[indexPath.row] taobaourl];
    itemVC.titleForWeb = @"宝贝详情";
}



#pragma mark Navigation Setting
- (void)setNavigation
{
    self.navigationItem.title = @"精选好物";
    
    UIButton *leftButton = [UIButton buttonWithType:UIButtonTypeCustom];
    leftButton.frame = CGRectMake(-20, 0, 40, 40);
    [leftButton setImage:[UIImage imageNamed:@"nav_back@2x.png"] forState:UIControlStateNormal];
    [self.view addSubview:leftButton];
    [leftButton addTarget:self action:@selector(leftBarButtonClick:) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.leftBarButtonItem = [[[UIBarButtonItem alloc] initWithCustomView:leftButton] autorelease];
    self.navigationController.navigationBar.translucent = NO;
}

#pragma mark Navigationbar ButtonAction
- (void)leftBarButtonClick:(UIBarButtonItem *)leftBarButton
{
    [self.navigationController popViewControllerAnimated:YES];
}




#pragma mark Internet Data
- (void)createData
{
    NSString *topUrl = @"http://open3.bantangapp.com/category/list";
    NSString *ViewUrl = @"http://open3.bantangapp.com/product/list";
    NSDictionary *topBody = [NSDictionary dictionaryWithObjectsAndKeys:@"9c1e6634ce1c5098e056628cd66a17a5", @"client_secret", @"bt_app_ios", @"client_id", nil];
    NSDictionary *ViewBody = [NSDictionary dictionaryWithObjectsAndKeys:@"bt_app_ios", @"client_id", @"9c1e6634ce1c5098e056628cd66a17a5", @"client_secret", nil];
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    [manager POST:topUrl parameters:topBody success:^(NSURLSessionDataTask * _Nonnull task, id  _Nonnull responseObject) {
        NSDictionary *dic = responseObject;
        self.dataArr = [NSMutableArray array];
        self.dataArr = [HZMSearchIcon baseModelByArr:dic[@"data"]];
//        [self.collection reloadData];
        
    } failure:^(NSURLSessionDataTask * _Nonnull task, NSError * _Nonnull error) {
        NSLog(@"%@", error);
        
    }];
    
    [manager POST:ViewUrl parameters:ViewBody success:^(NSURLSessionDataTask * _Nonnull task, id  _Nonnull responseObject) {
        NSDictionary *dic = responseObject;
        self.productArr = [NSMutableArray array];
        self.productArr = [HZMCollectionDetails baseModelByArr:dic[@"data"][@"product"]];
        [self.collection reloadData];
        
    } failure:^(NSURLSessionDataTask * _Nonnull task, NSError * _Nonnull error) {
        NSLog(@"%@", error);
        
    }];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
